package com.shop.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.shop.common.ResponseResult;
import com.shop.entity.Rights;
import com.shop.entity.vo.RightsVo;
import com.shop.mapper.RightsMapper;
import com.shop.service.RightsService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.List;

@Transactional
@Service
public class RightsServiceImpl implements RightsService {
    @Autowired
    private RightsMapper rightsMapper;
    @Override
    public ResponseResult selectRightsByList() {
        ResponseResult result = new ResponseResult();
        List<RightsVo> list = rightsMapper.selectPid();
        result.setSuccess("查询成功",list);
        return result;
    }

    @Override
    public ResponseResult selectRightsByTree() {
        ResponseResult result = new ResponseResult();
        QueryWrapper wrapper = new QueryWrapper();
        wrapper.eq("ps_level",'0');
        List<Rights> list = rightsMapper.selectList(wrapper);
        List<RightsVo> rightsVoList = new ArrayList<RightsVo>();
//        System.out.println(list);
        for (Rights rights:list) {
            RightsVo rightsVo = new RightsVo();
            rightsVo.setId(rights.getId());
            rightsVo.setAuthName(rights.getAuthName());
            rightsVo.setLevel(rights.getLevel());
            rightsVo.setPid(rights.getPid());
//            rightsVo.setPath(rights.getPath());
            QueryWrapper wrapper1 = new QueryWrapper();
            wrapper1.eq("ps_pid",rights.getId());
            List<Rights> list1 = rightsMapper.selectList(wrapper1);
            List<RightsVo> rightsVoList2 = new ArrayList<RightsVo>();
            for (Rights rights1:list1) {
                RightsVo rightsVo1 = new RightsVo();
                rightsVo1.setId(rights1.getId());
                rightsVo1.setAuthName(rights1.getAuthName());
                rightsVo1.setLevel(rights1.getLevel());
                rightsVo1.setPid(rights1.getPid());
//                rightsVo1.setPath(rights1.getPath());
                QueryWrapper wrapper2 = new QueryWrapper();
                wrapper2.eq("ps_pid",rights1.getId());
                List<Rights> list2 = rightsMapper.selectList(wrapper2);
                rightsVo1.setChildren(list2);
                rightsVoList2.add(rightsVo1);
            }
            rightsVo.setChildren(rightsVoList2);
            rightsVoList.add(rightsVo);
        }
        result.setSuccess("获取成功",rightsVoList);
        return result;
    }

    @Override
    public ResponseResult selectRightByTreeQuicker() {
        ResponseResult result =new ResponseResult();
        List<RightsVo> list = rightsMapper.selectPid();
        List<RightsVo> resultList = new ArrayList<>();
        for (RightsVo rightsVo:list){
            if (rightsVo.getLevel().equals("0")){
                List<RightsVo> secondList = new ArrayList<>();
                for (RightsVo rightsVo1:list){
                    if (rightsVo1.getPid()==rightsVo.getId()){
                        List<RightsVo> thirdList = new ArrayList<>();
                        for (RightsVo rightsVo2:list){
                            if (rightsVo2.getPid()==rightsVo1.getId()){
                                thirdList.add(rightsVo2);
                            }
                        }
                        rightsVo1.setChildren(thirdList);
                        secondList.add(rightsVo1);
                    }
                }
                rightsVo.setChildren(secondList);
                resultList.add(rightsVo);
            }
        }
        result.setSuccess("获取成功",resultList);
        return result;
    }

    @Override
    public ResponseResult selectMenu() {
        ResponseResult result = new ResponseResult();
        QueryWrapper wrapper = new QueryWrapper();
        wrapper.eq("ps_level",'0');
        List<Rights> list = rightsMapper.selectList(wrapper);
//        List<RightsVo> rightsVoList = new ArrayList<RightsVo>();
        RightsVo[] rightsVoList = new RightsVo[list.size()];
//        System.out.println(list);
        int i=-1;
        for (Rights rights:list) {
            i++;
            RightsVo rightsVo = new RightsVo();
            rightsVo.setId(rights.getId());
            rightsVo.setAuthName(rights.getAuthName());
            rightsVo.setLevel(rights.getLevel());
            rightsVo.setPid(rights.getPid());
//            rightsVo.setPath(rights.getPath());
            QueryWrapper wrapper1 = new QueryWrapper();
            wrapper1.eq("ps_pid",rights.getId());
            List<Rights> list1 = rightsMapper.selectList(wrapper1);
            List<RightsVo> rightsVoList2 = new ArrayList<RightsVo>();
            for (Rights rights1:list1) {
                RightsVo rightsVo1 = new RightsVo();
                rightsVo1.setId(rights1.getId());
                rightsVo1.setAuthName(rights1.getAuthName());
                rightsVo1.setLevel(rights1.getLevel());
                rightsVo1.setPid(rights1.getPid());
//                rightsVo1.setPath(rights1.getPath());
                QueryWrapper wrapper2 = new QueryWrapper();
                wrapper2.eq("ps_pid",rights1.getId());
                List<Rights> list2 = rightsMapper.selectList(wrapper2);
                rightsVo1.setChildren(list2);
                rightsVoList2.add(rightsVo1);
            }
            rightsVo.setChildren(rightsVoList2);
            rightsVoList[i] = rightsVo;
        }
        result.setSuccess("获取成功",rightsVoList);
        return result;
    }
    private Object[] ListToObject(List<RightsVo> list){
        Object [] tem = new Object[]{};
        int size = list.size();
        RightsVo[] arr = new RightsVo[size];
        tem = list.toArray(arr);//将转化后的数组放入已经创建好的对象中
        return  tem;
    }
}
